<?php
	class Manage_tracking extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_user");
			$this->load->model("apanel/db_news");
			$this->load->model("apanel/db_info");
			$this->load->model("apanel/db_tracking");
			$this->load->library('pagination');
		}
		
		public function index(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			redirect("manage_tracking/track_list");
		}
		
		public function track_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table1\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_tracking/datalist",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 4 ] },
			{ "bSortable": false, "aTargets": [ 5 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
		
			//== template
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_tracking/track_del_all"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/warranty/track_list.html');
		}
		
		public function datalist(){
			$output = $this->db_tracking->datalist();
			echo json_encode($output);
		}
		
	
		public function track_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("member_pkid","Member Name","required");
				$this->form_validation->set_rules("model_no","Model No","required");
				$this->form_validation->set_rules("serial_no","Serial No","required");
				$this->form_validation->set_rules("product_pkid","Product","required");
				$this->form_validation->set_rules("purchase_date","Purchase Date","required");
				$this->form_validation->set_rules("warranty_period","Warranty period","required");

				$this->form_validation->set_rules("claim_date","Claim Date","required");
				$this->form_validation->set_rules('return_date', 'Return Date', 'required');
	
				$this->form_validation->set_rules("remark","Remark","trim");
				$this->form_validation->set_rules("remark_return","Remark Return","trim");
				
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("remark",set_value("remark"));
					$this->cismarty->assign("remark_return",set_value('remark_return'));
			
						
					//set error
					$this->cismarty->assign("errormember_pkid",form_error('member_pkid'));
					$this->cismarty->assign("errormodel_no",form_error('model_no'));
					$this->cismarty->assign("errorserial_no",form_error('serial_no'));
					$this->cismarty->assign("errorproduct_pkid",form_error('product_pkid'));
					$this->cismarty->assign("errorpurchase_date",form_error('purchase_date'));
					$this->cismarty->assign("errorwarranty_period",form_error('warranty_period'));
					$this->cismarty->assign("errorclaim_date",form_error('claim_date'));
					$this->cismarty->assign("errorreturn_date",form_error('return_date'));

				}else {
					$data = array(
							"member_pkid"=>$this->input->post('member_pkid'),
							"model_no"=>$this->input->post('model_no'),
							"serial_no"=>$this->input->post('serial_no'),
							"product_pkid"=>$this->input->post('product_pkid'),
							"purchase_date"=>$this->input->post('purchase_date'),
							"warranty_period"=>$this->input->post('warranty_period'),
							"claim_date"=>$this->input->post('claim_date'),
							"return_date"=>$this->input->post('return_date'),
							"remark"=>$this->input->post('remark'),
							"remark_return"=>$this->input->post('remark_return')
					);
					$res = $this->db->insert('mobile_warranty_tracking',$data);
					if($res) redirect('manage_tracking/track_list/success');
					else  redirect('manage_tracking/track_list/fail');
				}
			
			}
		
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_tracking/track_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/warranty/track_add.html');
		}
		
		
		public function track_chk($searchData){
		//echo $searchData;
			//echo $group;
			function DateDiff($strDate1,$strDate2){            
				return (strtotime($strDate2) - strtotime($strDate1))/  ( 60 * 60 * 24*365 );
			}  
			
			if($searchData == "") $searchData="";
			$this->db->where('serial_no',$searchData);
		//	$this->db->select('card_no');
			$row = $this->db->get('mobile_warranty')->row();
			
	
		//	$this->db->select('card_no');
		
			
			if(count($row) > 0){
	
				if(DateDiff($row->purchase_date,date('Y-m-d H:i:s')) <= $row->warranty_period){
					$result = "<tr><td align='left'>Member Name : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='member_pkid' value='".$row->member_pkid."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>"; 
					$result .= "<tr><td align='left'>Model No : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='model_no' value='".$row->model_no."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>"; 
					$result .= "<tr><td align='left'>Serial No : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='serial_no' value='".$row->serial_no."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>"; 
					$result .= "<tr><td align='left'>Product : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='product_pkid' value='".$row->product_pkid."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>"; 
					$result .= "<tr><td align='left'>Purchase Date : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='purchase_date' value='".$row->purchase_date."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>";
					$result .= "<tr><td align='left'>Warranty Period : </td><span class='errors'>*</span><td class='hotelInfo'><input type='text' name='warranty_period' value='".$row->warranty_period."' readonly='readonly' /><font color='green'>Success ! </font></td></tr>";
					
				}else{
				
				$result="<font color='red'>Warranty Expired  !!</font>";
				}
					
			
			echo $result;
			
				
			}
		
		
		
		}
		
	
	public function track_detail($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			
			if($id != ""){
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_warranty_tracking');
				$row = $res->row();
			}
			if($this->input->post('bntSubmit') != null){
				
				
					$data = array(
					
							"status"=>$this->input->post("status"),
						
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('mobile_warranty_tracking',$data);
					if($res) redirect('manage_tracking/track_list/success');
					else  redirect('manage_tracking/track_list/fail');
	
					
			}else{
			
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_warranty_tracking');
				$row = $res->row();
				$this->cismarty->assign("model_no",$row->model_no);
				$this->cismarty->assign("serial_no",$row->serial_no);
				$this->cismarty->assign("purchase_date",$row->purchase_date);
				$this->cismarty->assign("warranty_period",$row->warranty_period);
				$this->cismarty->assign("claim_date",$row->claim_date);
				$this->cismarty->assign("return_date",$row->return_date);
				$this->cismarty->assign("remark",$row->remark);
				$this->cismarty->assign("remark_return",$row->remark_return);
				if($row->status == 1)
					 $this->cismarty->assign("sa",'checked="checked"');
				else 
					$this->cismarty->assign("sd",'checked="checked"');
					
				if($row->warranty_unit == 'Y')
					 $this->cismarty->assign("unit","Year");
				else 
					$this->cismarty->assign("unit","Month");
					
				$this->db->where("pkid",$row->member_pkid);
				$res1 = $this->db->get('mobile_member');
				$row1 = $res1->row();
				if($row1!=""){
				$this->cismarty->assign("name",$row1->name);
				}else{
				$this->cismarty->assign("name","");
				}
				
				$this->db->where("pkid",$row->product_pkid);
				$res2 = $this->db->get('mobile_product_item');
				$row2 = $res2->row();
				$this->cismarty->assign("name_en",$row2->name_en);
				
				
				
			}
			
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_tracking/track_detail/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/warranty/track_detail.html');
		}
		
		public function track_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_warranty_tracking");
				if($res) redirect("manage_tracking/track_list/success");
			}
		}
		
		public function track_del_all(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_warranty_tracking");
				}
			}
			redirect("manage_tracking/track_list/success");
		}
		
	}

?>